// pages/addstrationspecifications/addstrationspecifications.js
import basesup from '~/utils/supplier.js'
// createspec
Page({

    /**
     * 页面的初始数据
     */
    data: {
        store: 1,
        adddata: {
            name: '',
            params: []
        },
        addzi: false,
        ziname: ''
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },
    namechange(items) {
        let set = 'adddata.name'
        this.setData({
            [set]: items.detail
        })
    },
    openadd() {
        this.setData({
            addzi: true,
        })
    },
    onClose() {
        this.setData({
            addzi: false,
            ziname: ''
        })
    },
    delitem(items) {
        let setdatas = `adddata.params`
        let dellist = this.data.adddata.params;
        dellist.splice(items.currentTarget.dataset.select, 1)
        this.setData({
            [setdatas]: dellist
        });
    },
    addziconfin() {
        let zidata = {
            name: ''
        }
        if (this.data.ziname == '') {
            wx.showToast({
                title: '未输入子规格',
                icon: 'none',
                duration: 2000
            })
            return false
        } else {
            zidata.name = this.data.ziname
            let datalist = this.data.adddata.params
            datalist.push(zidata)
            this.setData({
                ['adddata.params']: datalist
            })
            this.onClose()
        }
    },
    async bindaddstrati() {
        if (this.data.adddata.name == '' || this.data.adddata.name == null || this.data.adddata.name == undefined) {
            wx.showToast({
                title: '未输入规格名称',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.adddata.params.length == 0) {
            wx.showToast({
                title: '未输入子规格',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let reqdata = this.data.adddata
        reqdata.params = JSON.stringify(reqdata.params)
        let resdata = await basesup.createspec(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '添加规格成功',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 2000);
        } else {
            wx.showToast({
                title: '添加失败，请重试！',
                icon: 'none',
                duration: 2000
            })
        }
    },
    async bindupdatastrati() {
        if (this.data.adddata.name == '' || this.data.adddata.name == null || this.data.adddata.name == undefined) {
            wx.showToast({
                title: '未输入规格名称',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (this.data.adddata.params.length == 0) {
            wx.showToast({
                title: '未输入子规格',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let reqdata = this.data.adddata
        reqdata.params = JSON.stringify(reqdata.params)
        let resdata = await basesup.updataspec(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '更改规格成功',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 2000);

        } else {
            wx.showToast({
                title: '更改失败，请重试！',
                icon: 'none',
                duration: 2000
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let initial = {
            name: '',
            params: []
        }
        if (JSON.stringify(options) != undefined && JSON.stringify(options).length > 2) {
            let setdata = 'adddata.params'
            let newparams = JSON.parse(JSON.parse(options.updata).params)
            this.setData({
                store: 2,
                adddata: JSON.parse(options.updata),
            })
            this.setData({
                [setdata]: newparams
            })
        } else {
            this.setData({
                store: 1,
                adddata: initial
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})